<?php
namespace Tiger\Api\Traits;
use App\Events\Register;
use App\Transformers\ApiError;
use App\Transformers\ProfileTransformer;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use App\Models\Profile;
use Tymon\JWTAuth\Facades\JWTAuth;
use App\Events\Login;


trait AuthenticatesApiUsers
{
    //注册用的参数，可覆盖
    public $register_parameters=['name','account','password','verify_code'];

    public $login_parameters=['account', 'password',];

    public $forget_parameters=['account','verify_code','new_password'];


    public $account_prefix='';

    public  $account='account';

    public $password='password';

    public $user_type='user';

    public $models='';

    public $role ='administrator';



    //退出登录
    public function logout_stub()
    {
        JWTAuth::invalidate(JWTAuth::getToken());
        return $this->apiReturnSuccess();
    }


    //注册接口
    public function register_stub( $request,\Closure $callback,$transformer)
    {
        //通用的参数检测函数
        $this->Check($request,$this->register_parameters);
        //其他校验信息hook
        //模型负责提供返回的函数
        if(is_callable($callback)) {
            $callback();
        }

        //只创建登陆账户
        $profile=Profile::createNewUser($this->user_type,$this->input,$this->account_prefix,0);
        //跟账户相关的在事件响应里面完成
        event(new Register($this->input,$profile));
        return  $this->apiReturn($profile,$transformer);
    }

    public function forget_stub( $request,\Closure $callback,$transformer)
    {
        //通用的参数检测函数
        $this->Check($request,$this->register_parameters);
        //其他校验信息hook
        if(is_callable($callback)) {
            $callback();
        }
        //模型负责创建用户信息
           $profile=Profile::findUserAccount($this->account_prefix.$this->input->mobile);
            if(empty($profile))
                $this->apiRestfulStoreError('用户未注册');
            $profile->password=bcrypt($this->input->new_password);
            $profile->save();
        return  $this->apiReturn($profile,$transformer);
    }

    public function login_stub( $request,\Closure $callback=null)
    {
        $this->Check($request,$this->login_parameters);
        if(is_callable($callback)) {
            $callback();
        }
        //尝试登陆
        if (!$token = JWTAuth::attempt([
                $this->account=>$this->account_prefix.$this->input->mobile,
                $this->password=>$this->input->{$this->password},
            ])) {
            ApiError::errorUnauthorized('用户名或密码错误');
        }
        //user status
        $profile = JWTAuth::toUser($token);
        event(new Login($profile));
        //response
        return  $this->apiReturn($profile,new ProfileTransformer());
    }

    public function forgetPassword_stub( $request,\Closure $callback=null)
    {
        //校验并提取参数
        $this->Check($request,$this->forget_parameters);
        //短信校验
        if(is_callable($callback)) {
            $callback();
        }
        //查找用户
        $profile=Profile::resetPassword($this->account_prefix.$this->input->mobile,$this->input);
        return  $this->apiReturn($profile,new ProfileTransformer());
    }


}